package com.rf.richfitwheel.admin.websocket.controller;

import com.rf.richfitwheel.admin.websocket.entity.ApiReturnObject;
import com.rf.richfitwheel.admin.websocket.entity.ApiReturnUtil;
import com.rf.richfitwheel.admin.websocket.server.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/websocket")
public class WebSocketController {

	private static Logger log = LoggerFactory.getLogger(WebSocketController.class);
    //页面请求
    @GetMapping("/socket/{cid}")
    public ModelAndView socket(@PathVariable String cid) {
        ModelAndView mav=new ModelAndView("/socket");
        mav.addObject("cid", cid);
        return mav;
    }
    //推送数据接口
    @ResponseBody
    @RequestMapping("/socket/push")
    public ApiReturnObject pushToWeb(@RequestParam Map<String, String> params) {
        try {
            WebSocketServer.sendInfo(params.get("message"),params.get("cid"));
        } catch (IOException e) {
            log.error("发送消息失败", e);
            return ApiReturnUtil.error(params.get("cid")+"#"+e.getMessage());
        }
        return ApiReturnUtil.success(params.get("cid"));
    }
}
